$(function() {
	var userInfo = '';
	var monthBillInfo = []; //存储每个月的账单信息

	function init() {
		var nowDate = new Date();
		var currentYear = nowDate.getFullYear(); //获取当前年份
		var currentMonth = nowDate.getMonth() + 1; //获取当前月份
		var currentTime = currentYear + '-' + currentMonth;
		$('#startTime').val(currentTime);
		$('#endTime').val(currentTime);
		//初始化月份选择器插件
		$('#startTime').MonthPicker();
		$('#endTime').MonthPicker();
		var regTime = /^((?:19|20)\d\d)-(0[1-9]|1[012])$/;
		$('#startTime').blur(function(){
			if(!regTime.test($(this).val())){
				$(this).val(currentTime);
				layer.msg("请输入YYYY-MM格式月份或直接点选月份");
			}
		})
		$('#endTime').blur(function(){
			if(!regTime.test($(this).val())){
				$(this).val(currentTime);
				layer.msg("请输入YYYY-MM格式月份或直接点选月份");
			}
		})

		userInfo = JSON.parse(decodeURI(decodeURI(sessionStorage.getItem('jf_user_info'))));
		if(userInfo) {
			console.log(userInfo)
			$('.accountName').html(userInfo.name)
			queryBill(userInfo);
			queryOneMonth(userInfo, parseInt(getTime().beginSettle));
		} else {
			window.location.href = "../index.html";
		}
	}
	init();
	/**
	 * 查询接口
	 * */
	function queryBill(userInfo) {
		var postData = {
			contract: userInfo.stores[0].contracts[0].uuid,
			beginSettle: getTime().beginSettle,
			endSettle: getTime().endSettle
		}

		function successFun(res) {
			$('#payTotal').html(res.data.payTotal);
			$('#payedTotal').html(res.data.payedTotal);
			$('#unpayedTotal').html(res.data.unpayedTotal);
			if(res.data.payTotal == 0) {
				$('#payRate').html('0.00%');
				$('#billList').empty().append('<p class="text-center f24">暂无数据</p>');
			} else {
				$('#billList').empty();
				$('#payRate').html(parseInt((res.data.payedTotal / res.data.payTotal) * 10000)/100 + '%');
			}
		}
		_http.queryBill(postData, successFun);
	}

	/**
	 * 获取开始时间和结束时间
	 * */
	function getTime() {
		return {
			beginSettle: $('#startTime').val().replace('-', ''),
			endSettle: $('#endTime').val().replace('-', '')
		}
	}

	/**
	 * 查询按钮点击事件
	 * */
	$('#queryBtn').click(function() {
		if(getTime().beginSettle > getTime().endSettle) {
			layer.msg('查询结束月份不能小于开始月份');
			return false;
		} else {
			layer.load(2, {
				shade: 0.3
			});
			$('#billList').empty();
			monthBillInfo.splice(0, monthBillInfo.length); //清空数组
			queryBill(userInfo);
			handleMonth(userInfo)
		}
	})

	/**
	 *分割月份，将开始时间和结束时间，划分成详细的单个月份 
	 * */
	function handleMonth(userInfo) {
		var beginSettle = parseInt(getTime().beginSettle);
		var endSettle = parseInt(getTime().endSettle);
		var itemTime = beginSettle;
		var isOver = false;
		for(var i = 0; i <= endSettle - beginSettle; i++) {
			if(parseInt(String(itemTime).substring(4, 6)) > 12) {
				itemTime = parseInt((parseInt(String(itemTime).substring(0, 4)) + 1) + '01');
			}
			console.log(itemTime)
			queryOneMonth(userInfo, itemTime)
			itemTime++;
			if(itemTime > endSettle) {
				break;
			}
		}

	}

	/**
	 * 查询每个月的账单情况
	 * */
	function queryOneMonth(userInfo, settleTime) {
		var postData = {
			contract: userInfo.stores[0].contracts[0].uuid,
			beginSettle: settleTime,
			endSettle: settleTime
		}

		function successFun(res) {
			if(res.data.subjects.length > 0) {
				$('#billList').append('<li class="col-lg-3 col-md-4 col-sm-6">' +
					'<div class="text-left">' +
					'<h3 class="text-center">' + String(settleTime).substring(0, 4) + '年' + String(settleTime).substring(4, 6) + '月</h3>' +
					'<p>应缴：' + res.data.payTotal + '元</p>' +
					'<p class="fc-darkgreen">已缴：' + res.data.payedTotal + '元</p>' +
					'<p class="fc-red">未缴：' + res.data.unpayedTotal + '元</p>' +
					'</div>' +
					'</li>')
				monthBillInfo.push(res);
			}
		}
		_http.queryBill(postData, successFun, null, true);
	}

	/**
	 * 账单点击事件，跳转到账单详情页
	 * */
	$('#billList').on('click', 'li', function() {
		var index = $(this).index();
		//sessionStorage.setItem('jf_selected_month',encodeURI(JSON.stringify(monthBillInfo[index])));
		window.location.href = "billDetail.html?month=" + $(this).children().find('h3').html();
	})

})